﻿#include "recfile_info.h"
#include "serial_file_com_nonebuf.h"
#define RECORD_IDX_INFO "RecordIdx.bin"

CRecfileInfo::CRecfileInfo()
{
}

CRecfileInfo::~CRecfileInfo()
{
}




uint32_t CRecfileInfo::GetInfoOffset(uint32_t idxNo)
{
	uint32_t offset = 0;

	offset = idxNo * m_infoSize + m_recInfoOffset;
	
	return offset;
}

int CRecfileInfo::CreateRecordInfo(const char* storage_path,uint32_t recblk_sz,uint32_t indexInfoSize, int indexNum)
{
	int ret = 0;
	ST_INDEX_FILE_INFO hdInfo;
	char idxMagic[8]    = STFS_INDEX_MAGIC;
	uchar_t * clearBuf  = NULL;
	int headInfoLen     = sizeof(ST_INDEX_FILE_INFO);
	char index_name[60] = {0};
	char file_path[50] = {0};
	//参数检查
	if(storage_path == NULL)
	{
		ret = ERR_NO_DEVICE;
		return ERR_NO_DEVICE;
	}
	if(-1 == access(storage_path,0))
	{
		return  ret = ERR_NO_DEVICE;;
	}
	if((0 == recblk_sz)||(0 == indexInfoSize)||(0 == indexNum) )
	{
		ret = ERR_DATA_SIZE;
		return ERR_DATA_SIZE;
	}

	CSerialFileComNobuf file;
	sprintf(file_path,"%s",RECORD_IDX_INFO);

	hdInfo.fileSize		  = INDEX_INFO_OFFSET +  indexInfoSize * indexNum * 3/2 ;
	ret = file.Open(file_path,0,SIZE_MB(10),SCSI_RDWR);
	if(ret < 0)
	{
		TRACE("Open record file %s  fail. ret %d \n",file_path,ret);
		return ret;
	}

	//初始索引信息
	memset(&hdInfo,0,headInfoLen);
	memcpy(hdInfo.idxMagic,idxMagic,8);
	hdInfo.version         = STFS_INDEX_VERSION;

	hdInfo.diskInfoOffset  = INDEX_DISK_INFO_OFFSET;
	hdInfo.diskInfoLen     = INDEX_DISK_INFO_LEN;
	hdInfo.recFileSize     = recblk_sz;
	hdInfo.indexInfoOffset = INDEX_INFO_OFFSET;
	hdInfo.indexLength     = indexInfoSize;
	hdInfo.indexInfoNum    = indexNum;

	hdInfo.recInfoOffset   = INDEX_BASEINFO_OFFSET;
	hdInfo.recInfoLen	   = sizeof(REC_SEGMENT_S);			//每条文件记录的长度（用于兼容不同地结构体）
	hdInfo.recInfoNum      = (indexInfoSize - INDEX_BASEINFO_OFFSET)/hdInfo.recInfoLen;
	hdInfo.baseRecFileLBA  = 0x1000;
	hdInfo.fileSize		   = INDEX_INFO_OFFSET +  indexInfoSize * indexNum ;
	//hdInfo.checkSum		= crc32((uchar_t*)&hdInfo,headInfoLen-4);


	//增加通道数支持，以备后续支持PC端播放器，检测出当前硬盘是录过几个通道的。
	hdInfo.sptMaxCH        = 2;

	time((time_t*)&hdInfo.createTm);

	TRACE_ERR("Stroage: Index offset %d, Index file size %d \n",hdInfo.indexInfoOffset,hdInfo.fileSize);

	//清理后面的数据
	int bufSize = SIZE_1MB;
	clearBuf = new uchar_t[bufSize];
	memset(clearBuf,0,bufSize);

	uint32_t writeSize = hdInfo.fileSize - INDEX_INFO_OFFSET;
	uint32_t ioSize    = bufSize;
	uint32_t offset    = hdInfo.indexInfoOffset;

	ret = file.Seek(offset,SCSI_SEEK_SET);
	//清空索引区的数据
	while(writeSize != 0)
	{
		ioSize = ((writeSize >(uint32_t) bufSize) ? bufSize:writeSize);
		ret = file.Write(clearBuf,ioSize);
		if(ret < 0)
		{
			TRACE_ERR("Stroage:ERR Index %s Index offset %d, Index file size %d \n",storage_path,hdInfo.indexInfoOffset,hdInfo.fileSize);
			goto ERR_OUT;
		}

		writeSize -= ioSize ;
	}

	if(clearBuf)
	{
		delete [] clearBuf;
	}
	//写入文件头
	ret = file.WritePos(0,(uchar_t *)&hdInfo,headInfoLen);
	if(ret < 0)
	{
		TRACE_ERR("Write head info fail.\n");
		file.Close();
		if(clearBuf)
		{
			delete [] clearBuf;
		}
		return ret;
	}
	TRACE_ERR("Stroage:Create com index file OK. \n");
	file.Close();
	TRACE_ERR("Close index file OK. \n");
	return 0;

ERR_OUT:
	if(clearBuf)
	{
		delete [] clearBuf;
	}
	TRACE_ERR("Stroage:create %s com index file fail.\n",index_name);
	file.Close();
	return ret;
}

int CRecfileInfo::StartRecInfo(uint32_t idxNo, uchar_t * data_buf)
{
	int ret = 0;
	return ret;
}

int CRecfileInfo::EndRecInfo(uint32_t idxNo,uchar_t * data_buf)
{
	int ret = 0;

	return ret;
}
